package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgSyncStockLogMapper;
import com.ygqh.baby.po.YgStock;
import com.ygqh.baby.po.YgSyncStockLog;
import com.ygqh.baby.po.YgSyncStockLogExample;
import com.ygqh.baby.po.YgSyncStockLogWithBLOBs;
import com.ygqh.baby.service.YgStockService;
import com.ygqh.baby.service.YgSyncStockLogService;

@Service
public class YgSyncStockLogServiceImpl implements YgSyncStockLogService {

	@Autowired
	private YgSyncStockLogMapper ygSyncStockLogMapper;
	@Autowired
	private YgStockService ygStockService;

	@Override
	public List<YgSyncStockLog> find() {
		YgSyncStockLogExample example = new YgSyncStockLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygSyncStockLogMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgSyncStockLogExample example = new YgSyncStockLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygSyncStockLogMapper.countByExample(example);
	}

	@Override
	public YgSyncStockLog findById(Long id) {
		return ygSyncStockLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgSyncStockLog ygSyncStockLog) {
		return ygSyncStockLogMapper.insertSelective(ygSyncStockLog);
	}

	@Override
	public int update(YgSyncStockLog ygSyncStockLog) {
		return ygSyncStockLogMapper.updateByPrimaryKeySelective(ygSyncStockLog);
	}

	@Override
	public ResultSet<YgSyncStockLog> search(QueryInfo queryInfo, String q) {
		List<YgSyncStockLog> list = ygSyncStockLogMapper.selectSuper(queryInfo, q);
		int count = ygSyncStockLogMapper.countSuper(q);
		ResultSet<YgSyncStockLog> resultSet = new ResultSet<YgSyncStockLog>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgSyncStockLogExample example = new YgSyncStockLogExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgSyncStockLog record = new YgSyncStockLog();
		record.setStatus(DataStatus.Delete);

		return ygSyncStockLogMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgSyncStockLog ygSyncStockLog) {
		int r = 0;
		if (ygSyncStockLog.getId() != null) {
			r = this.update(ygSyncStockLog);
		} else {
			r = this.save(ygSyncStockLog);
		}
		return r;
	}

	@Override
	public int addSyncStockLog(String oldS, String newS, String hanlder, String remark) {

		YgSyncStockLogWithBLOBs log = new YgSyncStockLogWithBLOBs();
		log.setSourceStock(oldS);
		log.setSyncStock(newS);
		log.setCreateBy(hanlder);
		log.setCreateTime(new Date());
		return this.save(log);
	}

	@Override
	public int addSyncStockLog(List<YgStock> stockList, String hanlder, String remark) {
		if (CollectionUtils.isEmpty(stockList)) {
			return 0;
		}
		List<String> skuCodes = stockList.stream().map(s -> s.getSkuCode()).collect(Collectors.toList());

		List<YgStock> oldStock = ygStockService.findBySkuCodes(skuCodes.toArray(new String[skuCodes.size()]));
		String oldS = JSON.toJSONString(oldStock);
		String newS = JSON.toJSONString(stockList);
		return this.addSyncStockLog(oldS, newS, hanlder, remark);
	}

}
